package com.baseframe.score.controller;


import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.score.entity.SnsScoreUser;
import com.baseframe.score.entity.request.SnsScoreUserRequest;
import com.baseframe.score.entity.vo.SnsScoreUserVO;
import com.baseframe.score.service.SnsScoreUserService;
import com.baseframe.tool.result.R;
import com.baseframe.tool.utils.date.DateUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 前台积分用户控制器
 */
@RestController
@RequestMapping("/front/score/user")
@AllArgsConstructor
public class FrontSnsScoreUserController {

    private SnsScoreUserService snsScoreUserService;

    /**
     * 用户积分详情
     * @return
     */
    @GetMapping("/details")
    public R<Object> getDetails(){
        SnsScoreUserVO userScoreDetails = snsScoreUserService.getUserScoreDetails();
        if(userScoreDetails == null){
            SnsScoreUser snsScoreUser = new SnsScoreUser();
            snsScoreUser.setUserId(AuthUtil.getUserId());
            snsScoreUser.setResidue(0);
            snsScoreUser.setAccrueTotal(0);
            boolean save = snsScoreUserService.save(snsScoreUser);
            if(!save){
                return R.fail("获取积分信息失败~");
            }
            userScoreDetails = snsScoreUserService.getUserScoreDetails();
        }
        //判断今天是否已经签到过了
        if(userScoreDetails.getLastSignInTime()==null){
            userScoreDetails.setHasSign(false);
        }else {
            userScoreDetails.setHasSign(DateUtil.isToday(userScoreDetails.getLastSignInTime()));
        }
        return R.data(userScoreDetails);
    }

}
